cmake_minimum_required(VERSION 3.16)

project("c-utils" 
        LANGUAGES C 
        VERSION 1.1.1.1
)

if(UNIX AND NOT APPLE)
        include(GNUInstallDirs)
endif()

if(NOT CMAKE_BUILD_TYPE)
        set(CMAKE_BUILD_TYPE Release)
endif()

set(LATEST_TAG "0.0.0")
#EXECUTE_PROCESS(COMMAND git --git-dir=${CMAKE_SOURCE_DIR}/.git describe --abbrev=0 --tags
#				TIMEOUT 5
#				OUTPUT_VARIABLE GIT_LATEST_TAG
#				OUTPUT_STRIP_TRAILING_WHITESPACE
#				)
#set(LATEST_TAG ${GIT_LATEST_TAG})
if(NOT DEFINED CMAKE_LIBRARY_OUTPUT_DIRECTORY)
        set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY  ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_INCLUDEDIR})
        set(CMAKE_LIBRARY_OUTPUT_DIRECTORY  ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
        set(CMAKE_RUNTIME_OUTPUT_DIRECTORY  ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
endif()

message("!!!!!!!!!!!!!!!!!!!!!!!")
message("!!!!!!!!!!!!!!!!!!!!!!!")
message("CMAKE_C_COMPILER:   ${CMAKE_C_COMPILER}")
message("CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")
message("CMAKE_BUILD_TYPE:   ${CMAKE_BUILD_TYPE}")
message("Build DIR:          ${CMAKE_BINARY_DIR}")
message("Latest Tag:         ${LATEST_TAG}")
message("VERSION:            ${PROJECT_VERSION}")
message("!!!!!!!!!!!!!!!!!!!!!!!")
message("!!!!!!!!!!!!!!!!!!!!!!!")

find_package(Threads REQUIRED)

file(GLOB_RECURSE MY_PUBLIC_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/ *.h)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC_FILES)

add_library(${PROJECT_NAME} ${SRC_FILES}  ${MY_PUBLIC_HEADERS})

set_target_properties(${PROJECT_NAME} PROPERTIES 
                                        C_STANDARD 11
                                        VERSION ${PROJECT_VERSION}
)

target_sources(${PROJECT_NAME} PUBLIC 
        FILE_SET HEADERS
        BASE_DIRS       inc
        FILES           "${MY_PUBLIC_HEADERS}"
)

target_link_libraries(${PROJECT_NAME}
        PUBLIC Threads::Threads
        PUBLIC rt
)

install(TARGETS ${PROJECT_NAME}
        EXPORT ${PROJECT_NAME}
        LIBRARY         DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME         DESTINATION ${CMAKE_INSTALL_BINDIR}
        FILE_SET HEADERS   DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)


include(CTest)
if(BUILD_TESTING)
        add_subdirectory(tests)
endif(BUILD_TESTING)

include(CPack)
set(PACKAGE ${PROJECT_NAME})
set(PACKAGE_BUGREPORT idoop@msn.cn)
set(PACKAGE_NAME ${PROJECT_NAME})
set(PACKAGE_VERSION ${CMAKE_PROJECT_VERSION})
set(PACKAGE_STRING "${PROJECT_NAME} ${CMAKE_PROJECT_VERSION}")
set(PACKAGE_TARNAME ${PROJECT_NAME})
set(PACKAGE_URL "")

include(CMakePackageConfigHelpers)

install(EXPORT ${PROJECT_NAME}
        FILE ${PROJECT_NAME}.cmake
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/c-utils
)